<?php

namespace app\admin\model\wanlshop;

use app\common\model\User;
use think\Model;

class Invoice extends Model
{
    // 表名
    protected $name = 'wanlshop_invoice';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'createtime_text',
        'status_text'
    ];

    protected $statusList = [
        '0' => '待审核',
        '1' => '审核通过',
        '2' => '审核驳回',
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['createtime'] ?? '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['sh'] ?? '');
        return $this->statusList[$value] ?? '';
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shopid', 'id');
    }
}
